﻿using Blogger.UI.Models;
using POC.Azure.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace Blogger.UI.Data
{
    public class BlogRepository :IBlogRepository
    {
        AzureAccount _account;
        BloggerContext db;
        public BlogRepository() 
        {
            Init(null);
        }
        public BlogRepository(AzureAccount account) 
        {
            Init(account);
        }
        public void Init(AzureAccount account)
        {
            _account = account ?? AzureConfig.AzureAccount;
            var tableClient = _account.CloudAccount.CreateCloudTableClient();
            db = new BloggerContext(tableClient);
            // per app
            db.CreateIfNotExists();
        }
        public List<Blog> GetBlogs()
        {
            var blogs = db.Blogs.ToList();
            return blogs;
        }

        public List<Blog> FindBlogsByTitle(string title)
        {
            throw new NotImplementedException();
        }

        public Blog FindBlog(string ID)
        {
            var query = db.Blogs.Where(x => x.ID == ID);
            return query.FirstOrDefault();
        }

        public Blog CreateBlog(Blog blog)
        {
            db.AddObject(blog);
            db.SaveChanges();
            return blog;
        }

        public Blog UpdateBlog(Blog blog)
        {
            var existingBlog = GetBlog(blog.ID);
            existingBlog.Body = blog.Body;
            existingBlog.Category = blog.Category;
            existingBlog.Title = blog.Title;
            existingBlog.User = blog.User;

            //db.AttachTo(BloggerContext.BlogSetName, blog);

            db.UpdateObject(existingBlog);
            db.SaveChanges();
            return blog;
        }

        public bool DeleteBlog(Blog blog)
        {
            var b = GetBlog(blog.ID);
            db.DeleteObject(b);
            db.SaveChanges();
            return true;
        }

        public Blog GetBlog(string blogId)
        {
            var query =  db.Blogs.Where(x => x.ID == blogId);
            return query.FirstOrDefault();
        }

        public void EditBlog(Blog blog)
        {
            var existingBlog = GetBlog(blog.ID);
            existingBlog.Body = blog.Body;
            existingBlog.Category = blog.Category;
            existingBlog.Title = blog.Title;
            existingBlog.User = blog.User;

            //db.AttachTo(BloggerContext.BlogSetName, blog);

            db.UpdateObject(existingBlog);
            db.SaveChanges();
        }

        public List<Posting> GetPostings(string blogId)
        {
            var posts = db.Postings.ToList();
            return posts;
        }

        public void CreatePosting(string blogId, Posting posting)
        {
            posting.BlogID = blogId.ToString();
            posting.Status = (int)PostStatus.New;
            db.AddObject(posting);
            db.SaveChanges();            
        }

        public Posting GetPosting(string postingId)
        {
            var query = db.Postings.Where(x => x.ID == postingId.ToString());
            return query.FirstOrDefault();
        }

        public void PublishPosting(Posting posting)
        {
            var existingPosting = GetPosting(posting.ID);
            existingPosting.Body = posting.Body;
            existingPosting.Category = posting.Category;
            existingPosting.Title = posting.Title;
            existingPosting.Status = (int)PostStatus.Published;
            db.UpdateObject(existingPosting);
            db.SaveChanges();
        }

        public void EditPosting(Posting posting)
        {
            var existingPosting = GetPosting(posting.ID);
            existingPosting.Body = posting.Body;
            existingPosting.Category = posting.Category;
            existingPosting.Title = posting.Title;
            existingPosting.Status = (int)PostStatus.Updated;
            db.UpdateObject(existingPosting);
            db.SaveChanges();           
        }

        public void DeletePosting(Posting posting)
        {
            var existingPosting = GetPosting(posting.ID);
            db.DeleteObject(existingPosting);
            db.SaveChanges();
        }


        public List<Posting> GetPostingsByBlogID(string blogId)
        {
            var query = db.Postings.Where(x => x.BlogID == blogId);
            return query.ToList();
        }
    }
    public class PostingRepository : IPostingRepository
    {
        AzureAccount _account;
        BloggerContext db;
        public PostingRepository() 
        {
            Init(null);
        }
        public PostingRepository(AzureAccount account) 
        {
            Init(account);
        }
        public void Init(AzureAccount account)
        {
            _account = account ?? new AzureAccount(true);
            var tableClient = _account.CloudAccount.CreateCloudTableClient();
            db = new BloggerContext(tableClient);
            // per app
            db.CreateIfNotExists();
        }

        public void EditPosting(Posting posting)
        {
            posting.Status = (int)PostStatus.Updated;
            db.UpdateObject(posting);
            db.SaveChanges();
        }

        public List<Posting> GetPostings()
        {
            var posts = db.Postings.ToList();
            return posts;
        }

        public List<Posting> GetPostingsByBlog(string BlogID)
        {
            throw new NotImplementedException();
        }

        public List<Posting> FindPostingsByTitle(string title)
        {
            throw new NotImplementedException();
        }

        public Posting FindPosting(string ID)
        {
            var query = db.Postings.Where(x => x.ID == ID);
            return query.FirstOrDefault();
        }

        public Posting CreatePosting(Posting posting)
        {
            posting.Status = (int)PostStatus.New;
            db.AddObject(posting);
            db.SaveChanges();
            return posting;
        }

        public Posting UpdatePosting(Posting posting)
        {
            posting.Status = (int)PostStatus.Updated;
            db.UpdateObject(posting);
            db.SaveChanges();
            return posting;
        }

        public bool DeletePosting(Posting posting)
        {
            db.DeleteObject(posting);
            db.SaveChanges();
            return true;
        }
    }
}